package com.wang.web3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wang.web3.base.Constant;
import com.wang.web3.bean.TianWallet;
import com.wang.web3.bean.TianWallets;
import com.wang.web3.bean.UserBean;
import com.wang.web3.data.service.ICallBackListener;
import com.wang.web3.data.service.impl.ApiServiceImpl;
import com.wang.web3.utils.SPUtils;

import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.RequestBody;


public class LockWalletActivity extends AppCompatActivity {

    EditText etaddress,etnickname,etmail;
    ImageView ivback;

    Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_wallet);
        String address = getIntent().getStringExtra("address");
        etaddress = findViewById(R.id.walletAdress);
        etnickname = findViewById(R.id.nickname);
        etmail = findViewById(R.id.mail);
        ivback = findViewById(R.id.mLeftIv);
        ivback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        etaddress.setText(address + "");
    }

    public void lock(View view){
        final String address = etaddress.getText().toString();
        String nickname = etnickname.getText().toString();
        String mail = etmail.getText().toString();
        if (address.equals("") || nickname.equals("") || mail.equals("")){
            Toast.makeText(this,"以上信息必填。",Toast.LENGTH_SHORT).show();
            return;
        }
        UserBean userBean = new UserBean(address,mail,nickname);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),gson.toJson(userBean));

        ApiServiceImpl.getInstence().userSave(body, new ICallBackListener() {
            @Override
            public void onSuccess(String string) {
                Toast.makeText(LockWalletActivity.this,"绑定成功。",Toast.LENGTH_SHORT).show();

                TianWallets wallets;

                String ss = (String) SPUtils.get(LockWalletActivity.this, Constant.WalletKey,"");

                if (!ss.equals("")){
                    wallets = gson.fromJson(ss, TianWallets.class);
                }else {
                    wallets = new TianWallets(new ArrayList<TianWallet>());
                }

                for (int j = 0; j < wallets.getTianWallets().size(); j++) {
                    if ((address).equals(wallets.getTianWallets().get(j).getAdress())){
                        wallets.getTianWallets().get(j).setInfo("该账号已绑定");

                        SPUtils.put(LockWalletActivity.this,Constant.WALLETLOCKADDRESS,gson.toJson(wallets.getTianWallets().get(j)));
                        continue;
                    }
                }
                SPUtils.put(LockWalletActivity.this,Constant.WalletKey,gson.toJson(wallets));

                setResult(0,new Intent());
                finish();
            }

            @Override
            public void onFaild(String string) {
                Toast.makeText(LockWalletActivity.this,"。" + string,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
